<?php
/**
*	last change: 2011-03-16
*       Available under the GFDL (http://www.gnu.org/copyleft/fdl.html) as source code was taken from  http://www.mediawiki.org/wiki/Extension:VideoFlash
*       Modification by SignpostMarv Martin
*       html5 added by Frank Forte
*/ 
	
$wgExtensionFunctions[] = 'wfVideoFlash';
$wgExtensionCredits['parserhook'][] = array(
	'name' => 'VideoFlash',
	'description' => 'Allows to display videos from YouTube, Google Video, Dailymotion, Sevenload, Revver and similar services',
	'author' => 'Alberto Sarullo, SignpostMarv Martin, Frank Forte',
	'version' => '1.2.4',
	'url' => 'https://www.mediawiki.org/wiki/Extension:VideoFlash'
);

function wfVideoFlash() {
	global $wgParser;
	$wgParser->setHook('Media', 'renderMedia');
}

# The callback function for converting the input text to HTML output
function renderMedia($input, $args) {
	// append new services to the array with the key being the intended value of the type attribute on the <videoflash> tag
	$url = array();
	$url['flash'] = '%1$s';
	$url['html5'] = '%1$s';

	// if the embed code for a service requires flashvars attributes, you can add them here
	$flashvars = array();
	$flashvars['revver'] = 'mediaId=%1$u&affiliateId=0';

	$type       = isset($args['type'],$url[$args['type']]) ? $args['type'] : 'html5';
	$media_url  = isset($url[$type]) ? $url[$type] : $url['flash'];
	$flash_vars = isset($flashvars[$type]) ? $flashvars[$type] : '';

	$input_array = explode('|', htmlspecialchars($input));
	$id     = current($input_array);
	$width  = (count($input_array) > 1 && is_numeric($input_array[1])) ? $input_array[1] : 425;
	$height = (count($input_array) > 2 && is_numeric($input_array[2])) ? $input_array[2] : 350;
	$fullscreen = (isset($args['fullscreen']) ? $args['fullscreen'] : 'true') === 'false' ? false : true;

		if(strtolower($type) == 'html5')
		{
			// I recommend CSS to set the video size, i.e. <videoflash type="html5" style="width:200px;height:150px">url</videoflash>
			$output = '<div class="video"><video';
			foreach($args as $attribute=>$value)
			{
				 $output .= ' '.$attribute.'="'.$value.'"';
			}
			
		     $output .= ' width="'.$width.'" height="'.$height.'" controls="true"><source src="'.$id.'"></video></div>';
		     return $output;
		}
		else
		{
			 $output = '<object width="%2$u" height="%3$u">'
		                .' <param name="movie" value="'.$url[$type].'" />'
		                .' <param name="allowFullScreen" value="%4$s" />'
		                .' <param name="wmode" value="transparent" />'
		                .' <embed src="'.$url[$type] . '" type="application/x-shockwave-flash" wmode="transparent"'
		                .' width="%2$u" height="%3$u" allowfullscreen="%4$s"'
		                        . ' flashvars="' . $flash_vars . '"></embed></object>';
			 return sprintf($output,$id,$width,$height,$fullscreen ? 'true' : 'false', (integer)$fullscreen);
		}
}